<?php
/**
 * 根据麦子学院微信开发高级教程
 * 练习用户管理接口使用
 */

namespace Home\Controller;
use Think\Controller;
use Com\Wechat;
use Com\WechatAuth;

header("Content-type:text/html;charset=utf-8;");
class UserController extends Controller {
    private $appid = 'wxb70359eb19266f10';
    private $appsecret = 'd05e862df76a7401e0b2af0f06e8eade';
    private $WechatAuth = '';
    private $access_token = '';


    public function __construct(){
        parent::__construct();
        if(!session('token')){
            $this->WechatAuth = new WechatAuth($this->appid, $this->appsecret); //初始化WechatAuth类
            $WechatAuth = $this->WechatAuth;
            $token = $WechatAuth->getAccessToken(); //获取access_token数组
            session(array('expire'=>$token['expire_in'])); //设置缓存时间
            session('token', $token['access_token']); //缓存token
            $this->access_token = $token['access_token'];
        }else{
            $token = session('token');
            $this->WechatAuth = new WechatAuth($this->appid, $this->appsecret, $token);
            $this->access_token = $token;
        }
        
    }

    //获取所有用户组
    public function selectGroupsGet(){
        header("Content-type:text/html;charset=utf-8;");
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->groupsGet();
        var_dump($data);
    }

    //创建用户分组
    public function createGroup(){
        header("Content-type:text/html;charset=utf-8;");
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->groupsCreate('前端开发组');
        var_dump($data);
    }

    //删除用户分组
    public function deleteGroup(){
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->groupsDelete(103);
        var_dump($data);
    }

    //查询用户所在的分组
    public function groupId(){
        $user = M('Users')->find(4);
        $openid = $user['openid'];
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->groupsGetid($openid);
        var_dump($data);
    }

    //修改用户分组
    public function updateGroup(){
        header("Content-type:text/html;charset=utf-8;");
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->groupsUpdate(101,'前端开发组');
        var_dump($data);
    }

    //移动用户分组
    public function moveGroup(){
        header("Content-type:text/html;charset=utf-8;");
        $user = M('Users')->find(4);
        $openid = $user['openid'];
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->groupsMemberUpdate($openid, 100);
        var_dump($data);
    }

    // 获取指定用户基本信息
    public function getUserInfo(){
        header("Content-type:text/html;charset=utf-8;");
        $user = M('Users')->find(4);
        $openid = $user['openid'];
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->userInfo($openid);
        dump($data);
    }

    // 获取用户列表
    public function getUserList(){
        header("Content-type:text/html;charset=utf-8;");
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->userGet();
        dump($data);
    }

    // 设置用户备注名
    public function setUserRemark(){
        $user = M('Users')->find(4);
        $openid = $user['openid'];
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->userInfoUpdateremark($openid, 'php开发-明亮');
        dump($data);

    }

    // 创建自定义菜单
    public function createMenu(){

        $menudata = array(
                        array(
                            "type"=>"click",
                            "name"=>"今日歌曲",
                            "key"=>"V1001_TODAY_MUSIC"
                            ),
                        array(
                            "name"=>"菜单",
                            "sub_button"=>array(
                                array(
                                    "type"=>"view",
                                    "name"=>"搜索",
                                    "url"=>"http://www.soso.com/"
                                    ),
                                array(
                                    "type"=>"view",
                                    "name"=>"视频",
                                    "url"=>"http://v.qq.com/"
                                    ),
                                array(
                                    "type"=>"click", 
                                    "name"=>"赞一下我们", 
                                    "key"=>"V1001_GOOD"
                                    )
                                )
                            )
                        );

        $menu_extend = array(
                        array(
                            "name"=>"扫码",
                            "sub_button"=>array(
                                array(
                                    "type"=>"scancode_waitmsg",
                                    "name"=>"扫码带提示",
                                    "key"=>"rselfmenu_0_0",
                                    ),
                                array(
                                    "type"=>"scancode_push",
                                    "name"=>"扫码推事件",
                                    "key"=>"rselfmenu_0_1",
                                    )
                                )
                            ),
                        array(
                            "name"=>"发图",
                            "sub_button"=>array(
                                array(
                                    "type"=>"pic_sysphoto",
                                    "name"=>"系统拍照发图",
                                    "key"=>"rselfmenu_1_0",
                                    ),
                                array(
                                    "type"=>"pic_photo_or_album",
                                    "name"=>"拍照或者相册发图",
                                    "key"=>"rselfmenu_1_1",
                                    ),
                                array(
                                    "type"=>"pic_weixin",
                                    "name"=>"微信相册发图",
                                    "key"=>"rselfmenu_1_2",
                                    )
                                )
                            ),
                        array(
                            "name"=>"发送位置",
                            "type"=>"location_select",
                            "key"=>"rselfmenu_2_0"
                            ),
                        );

        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->menuCreate($menu_extend);
        dump($data);
    }

    // 获取所有的自定义菜单
    public function getMenu(){
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->menuGet();
        dump($data);
    }

    public function deleteMenu(){
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->menuDelete();
        dump($data);
    }

    // 打印输出access_token
    public function dumpToken(){
        echo $this->access_token;
    }
}